package com.fqgj.sentry.manage;

import com.fqgj.sentry.common.exception.BizException;
import com.fqgj.sentry.common.exception.enums.ErrorCodeEnums;
import com.fqgj.sentry.common.utils.HashUtils;
import com.fqgj.sentry.manage.dao.McUserDao;
import com.fqgj.sentry.manage.entity.McUserEntity;
import com.fqgj.sentry.manage.service.BaseInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Created by ykpbean kangping.ying@yuntu-inc.com
 *
 * @description
 * @create 2017-07-15 下午10:41
 */

@Service
public class BaseInfoServiceImpl implements BaseInfoService {
    @Autowired
    private McUserDao mcUserDao;

    @Override
    public McUserEntity selectBaseInfo(Long userId) {
        McUserEntity userEntity = mcUserDao.selectByPrimaryKey(userId);
        if (null == userEntity || userEntity.getDeleted()) {
            throw new BizException(ErrorCodeEnums.user_not_exist_error);
        }
        return userEntity;
    }

    @Override
    public Boolean updateBaseInfo(McUserEntity userEntity) {
        mcUserDao.updateByPrimaryKey(userEntity);
        return true;
    }

    @Override
    public Boolean modifyPassWord(Long userId, String newPassWord, String oldPassWord) {
        McUserEntity userEntity = mcUserDao.selectByPrimaryKey(userId);
        if (null == userEntity || userEntity.getDeleted()) {
            throw new BizException(ErrorCodeEnums.user_not_exist_error);
        }
        if (!userEntity.getPassword().equals(HashUtils.md5(oldPassWord))) {
            throw new BizException(ErrorCodeEnums.password_error, "原密码错误");
        }
        userEntity.setPassword(HashUtils.md5(newPassWord))
                   .setGmtModified(new Date());
        mcUserDao.updateByPrimaryKey(userEntity);
        return true;
    }

}
